package com.nondo.dean.product.service;



import java.util.ArrayList;
import java.util.List;

import com.nondo.dean.product.common.CategoryUtils;
import com.nondo.dean.product.domain.*;
import com.nondo.dean.product.mapper.ProductExtendsMapper;
import com.nondo.dean.product.mapper.ProductMapper;
import com.nondo.dean.product.mapper.SeriesMapper;
import com.nondo.dean.product.model.SeriesBean;
import com.nondo.dean.product.model.result.ProductBeanResult;
import com.nondo.dean.product.model.result.ProductResult;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.product.mapper.ProductCategoryMapper;
import com.nondo.dean.product.model.CategoryBean;
import com.nondo.dean.product.model.result.CategoryResult;

@Component
public class ProductCategoryService {

    @Autowired
    private ProductCategoryMapper  productCategoryMapper;

    @Autowired
    private SeriesMapper seriesMapper;

    @Autowired
    private ProductMapper productMapper;
    
    @Autowired
    AliyunOssTool aliyunOssTool;

    @Autowired
    ProductExtendsMapper productExtendsMapper;



    /**
     * 插入产品分类
     * @param productCategory
     * @return
     */
    @Transactional
    public int addProductCategory(ProductCategory productCategory) {
        return productCategoryMapper.insertSelective(productCategory);
    }

    /**
     * 修改产品分类
     * @param productCategory
     * @return
     */
    @Transactional
    public int updateProductCategory(ProductCategory productCategory) {
        return productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    /**
     * 按照主键ID查询产品分类
     * @param id
     * @return
     */
    public ProductCategory selectProductCategory(Integer id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 产品分类分页查询列表
     * @param offset
     * @param limit
     * @param productCategory
     * @return
     */
    public List<ProductCategory> getProductCategoryList(int offset, int limit, ProductCategory productCategory) {
        ProductCategoryExample example = new ProductCategoryExample();
        example.setOrderByClause("order_num ASC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        ProductCategoryExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productCategory);
        return productCategoryMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 所有产品分类
     * @param
     * @param
     * @param
     * @return List<ProductCategory>
     */
    public List<ProductCategory> categoryList() {
        ProductCategoryExample example = new ProductCategoryExample();
        example.setOrderByClause("order_num ASC");
        example.createCriteria().andDelFlagEqualTo(Product.DEL_FLAG_NORMAL);
        return productCategoryMapper.selectByExample(example);
    }

    /**
     * 筛选产品表二级分类id 不为空的二级分类
     * @return
     */
    public List<ProductCategory> queryCategory(){
       return productExtendsMapper.queryProductCategory();
    }

    /**
     * 过滤没有产品后的分类列表
     * @return
     */
    public List<CategoryResult> getCategoryAll(){
        return CategoryUtils.handle(categoryList(),queryCategory(),aliyunOssTool);
    }

    /**
     * 产品分类总数统计
     * @param productCategory
     * @return
     */
    public Long count(ProductCategory productCategory) {
        ProductCategoryExample example = new ProductCategoryExample();
        ProductCategoryExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productCategory);
        return productCategoryMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param productCategory
     */
    void setCriteria(ProductCategoryExample.Criteria criteria, ProductCategory productCategory) {
        if (productCategory != null) {
            if (productCategory.getId() != null)
                criteria.andIdEqualTo(productCategory.getId());
            if (productCategory.getPid() != null)
                criteria.andPidEqualTo(productCategory.getPid());
            if (StringUtils.isNoneBlank(productCategory.getName()))
                criteria.andNameLike("%" + productCategory.getName() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

   /**
    * 按照产品分类名称查询产品分类
    * @param name
    * @return
    */
    public ProductCategory selectProductCategoryByName(String name) {
        ProductCategoryExample example = new ProductCategoryExample();
        ProductCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<ProductCategory> productCategoryList = productCategoryMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(productCategoryList)?productCategoryList.get(0):null;
    }
    
    /**
     * 按照父分类ID查询产品分类
     * @param name
     * @return
     */
     public List<ProductCategory> selectProductCategoryById(Integer pid) {
         ProductCategoryExample example = new ProductCategoryExample();
         example.setOrderByClause("order_num ASC");
         ProductCategoryExample.Criteria criteria = example.createCriteria();
         criteria.andPidEqualTo(pid);
         criteria.andDelFlagEqualTo(String.valueOf(0));
         List<ProductCategory> productCategoryList = productCategoryMapper.selectByExample(example);
         return productCategoryList;
     }

    /**
     * 查询分类详情
     * @param id
     * @return 父类和所对应的子类列表 {@link CategoryResult}
     */
    public CategoryResult getCategoryDetail(Integer id) {
        CategoryResult categoryResult = new CategoryResult();
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        CategoryBean categoryBean = CategoryBean.covertToBean(productCategory);
        categoryBean.setImage(aliyunOssTool.getFileUrl(categoryBean.getImage()));
        categoryResult.setId(categoryBean.getId());
        categoryResult.setName(categoryBean.getName());
        categoryResult.setImage(categoryBean.getImage());
        categoryResult.setOrderNum(categoryBean.getOrderNum());
        List<CategoryBean> subList = new ArrayList<CategoryBean>();
        Integer pid = productCategory.getId();
        List<ProductCategory> productCategoryList = selectProductCategoryById(pid);
        for (ProductCategory productCategoryDb : productCategoryList) {
            CategoryBean subCategoryBean = CategoryBean.covertToBean(productCategoryDb);
            subCategoryBean.setImage(aliyunOssTool.getFileUrl(subCategoryBean.getImage()));
            subList.add(subCategoryBean);
        }
        categoryResult.setChildrenList(subList);
        return categoryResult;
    }

    /**
     * 返回所有分类数据
     * @return
     */
    public List<CategoryResult> getCategoryDetailAll(){
        List<CategoryResult> categoryResultList=new ArrayList<CategoryResult>();
        ProductCategoryExample productCategoryExample=new ProductCategoryExample();
        productCategoryExample.setOrderByClause("order_num");
        ProductCategoryExample.Criteria criteria= productCategoryExample.createCriteria();
        criteria.andDelFlagEqualTo(String.valueOf(0)).andPidEqualTo(0);
        List<ProductCategory> productCategoryList= productCategoryMapper.selectByExample(productCategoryExample);
        for(ProductCategory productCategory : productCategoryList){
            categoryResultList.add(getCategoryDetail(productCategory.getId()));
        }
       return categoryResultList;
    }

    /**
     * 返回所有系列
     * @return
     */
    public List<SeriesBean> getSeriesAll(Integer categoryId){
        List<SeriesBean> list=new ArrayList<>();
        List<Series> seriesList=productExtendsMapper.selectSeriesAll(categoryId);
        Series beans=new Series();
        beans.setId(0);
        beans.setName("全部");
        seriesList.add(0,beans);
        for(Series series :seriesList){
            SeriesBean seriesBean= ProductBeanResult.covertToBean(series);
            seriesBean.setImage(aliyunOssTool.getImgUrl(seriesBean.getImage()));
            list.add(seriesBean);
        }

        return list;
    }

    /**
     * categoryId不为null seriesId不为null 按系列和分类查询产品
     * @param categoryId
     * @param seriesId
     * @param offset
     * @param limit
     * @return
     */
    public  List<Product> queryProduct(Integer categoryId, Integer seriesId,int offset, int limit){
        ProductExample productExample=new ProductExample();
        RowBounds rowBounds = new RowBounds(offset, limit);
        if(categoryId !=null && seriesId !=null ) {
            //按分类和系列查询产品
            productExample.createCriteria().andCategoryIdEqualTo(categoryId).andSeriesIdEqualTo(seriesId).andDelFlagEqualTo(ProductCategory.DEL_FLAG_NORMAL).andShelfStatusEqualTo(1);
        }else{
            //按系列查询产品
            productExample.createCriteria().andSeriesIdEqualTo(seriesId).andDelFlagEqualTo(ProductCategory.DEL_FLAG_NORMAL).andShelfStatusEqualTo(1);
        }
        return productMapper.selectByExampleWithRowbounds(productExample,rowBounds);
    }

    /**
     * 按二级分类id查找产品   categoryId 不为空 seriesId不为空 按二级分类和系列查询对应产品
     * @param categoryId
     * @param offset
     * @param limit
     * @return
     */
    public List<Product>  queryCategoryProduct(Integer categoryId,Integer seriesId,int offset, int limit){
        ProductExample productExample=new ProductExample();
        RowBounds rowBounds = new RowBounds(offset, limit);
        if(categoryId !=null && seriesId != null){
            productExample.createCriteria().andCategoryIdEqualTo(categoryId).andSeriesIdEqualTo(seriesId).andDelFlagEqualTo(ProductCategory.DEL_FLAG_NORMAL).andShelfStatusEqualTo(1);
        }else {
            productExample.createCriteria().andCategoryIdEqualTo(categoryId).andDelFlagEqualTo(ProductCategory.DEL_FLAG_NORMAL).andShelfStatusEqualTo(1);
        }
        return productMapper.selectByExampleWithRowbounds(productExample,rowBounds);
    }
    /**
     * @param categoryId 二级分类id
     * @param  seriesId 系列id
     * 返回分类和系列所对应的产品数据
     * @return
     */
    public  List<ProductBeanResult> productClassification(int offset, int limit, Integer categoryId, Integer seriesId){
        List<ProductBeanResult> newList=new ArrayList<>();
        ProductBeanResult beanResult =new ProductBeanResult();
        newList.add(beanResult);
        if(categoryId !=null && seriesId ==0){
            //获取所有系列
            beanResult.setSeriesList(getSeriesAll(categoryId));
           //获取二级分类所对应的产品
            List<ProductResult> productResultList =new ArrayList<>();
            List<Product> productList= queryCategoryProduct(categoryId,null,offset,limit);
            for(Product pd : productList){
                ProductResult bean= ProductResult.covertToBean(pd);
                bean.setMainImage(aliyunOssTool.getImgUrl(bean.getMainImage()));
                productResultList.add(bean);
            }
            beanResult.setProductResultList(productResultList);
        }else if(categoryId !=null && seriesId != null){
            //获取所有系列
            beanResult.setSeriesList(getSeriesAll(categoryId));
            //获取分类系列对应的产品
            List<ProductResult> productResultList =new ArrayList<>();
            List<Product> productList=queryProduct(categoryId,seriesId,offset,limit);
            for(Product productResult : productList){
                ProductResult bean= ProductResult.covertToBean(productResult);
                bean.setMainImage(aliyunOssTool.getImgUrl(bean.getMainImage()));
                productResultList.add(bean);
            }
            beanResult.setProductResultList(productResultList);
        }


        return newList;
    }

    /**
     * 删除产品分类
     * @param id
     */
    @Transactional
    public void deleteProductCategory(Integer id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        productCategory.setDelFlag(ProductCategory.DEL_FLAG_DELETE);
        productCategoryMapper.updateByPrimaryKey(productCategory);
        List<ProductCategory> subProductCategoryList = selectProductCategoryById(productCategory.getId());
        for (ProductCategory subProductCategory : subProductCategoryList) {
            subProductCategory.setDelFlag(ProductCategory.DEL_FLAG_DELETE);
            productCategoryMapper.updateByPrimaryKey(subProductCategory);
        }
    }
}
